﻿// 界面主要功能区

using AdjPlane;
using LevelNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlaneADJ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location = new System.Drawing.Point(300, 100);
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            popt_.WgtDef = Weight.hmt2;
            popt_.Eps_VCE = 0.01;
            popt_.Eps_X = 0.1;
            popt_.IterMax_VCE = 20;
            popt_.IterMax_X = 30;
            popt_.DisWgt = 1;
            popt_.AveDis = 1;
            popt_.UseKnApprox = 0;
            popt_.Approx_Trilateral = 1;
            popt_.GyOpt.UseGyro = 0;
            popt_.GyOpt.GyInAdp = 0;
            popt_.GyOpt.GyOuAdp = 1;
            textBox5.Enabled = false;
        }

        //平面网平差参数
        public static Proc_t popt_;
        public static File_t fopt_;

        //进度条
        Form3 fm3;
        void StartPro()
        {
            fm3 = new Form3();
            fm3.ShowDialog();
        }
        void EndPro(string sec)
        {
            fm3.End(sec);
        }
        void FailPro()
        {
            fm3.Fail();
        }

        Form2 form2 = new Form2();
        private void 设置参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (form2 == null || form2.IsDisposed)
                form2 = new Form2();
            form2.Show();
            form2.Focus();
        }


        private void 近似坐标计算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Approx();
        }

        private void 经验定权平差ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cal_PlaneNet(Weight.Tradition);
        }

        private void 方差分量估计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (popt_.WgtDef == Weight.Tradition)
                Cal_PlaneNet(Weight.hmt2);
            else 
                Cal_PlaneNet(popt_.WgtDef);
        }

        private void 自由网平差ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 经验定权ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Cal_FreeNet(Weight.Tradition);
        }

        private void 方差分量估计ToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            Cal_FreeNet(popt_.WgtDef);
        }

        void ShowPath()
        {
            textBox1.Text = fopt_.infile;
            textBox2.Text = fopt_.approx_3s;
            textBox3.Text = fopt_.oufile;
            textBox4.Text = fopt_.mapfile;
        }

        /// <summary>
        /// 平面网平差
        /// </summary>
        /// <param name="o">经验定权或者赫尔默特</param>
        void Cal_PlaneNet(object o)
        {
            Weight cov = (Weight)o;
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Multiselect = false;
            op.Filter = "Text Files(.in2)|*.in2";
            op.Title = "选择文件";
            op.ShowDialog(this);
            string path = op.FileName;
            if (string.IsNullOrWhiteSpace(path)) { }
            else
            {
                try
                {
                    Thread th = new Thread(StartPro);//开启进程
                    th.IsBackground = true;
                    th.Start();
                    Stopwatch spw = new Stopwatch();
                    spw.Start();
                    Plane pn = new Plane();
                    fopt_.infile = path;
                    popt_.WgtDef = cov;
                    if (popt_.GyOpt.UseGyro == 1) 
                        popt_.WgtDef = Weight.Tradition;
                    pn.PlaneAdj(popt_, ref fopt_);
                    
                    spw.Stop();
                    TimeSpan ts = spw.Elapsed;

                    if (fopt_.oufile != "")
                    {
                        Process.Start("notepad.exe", fopt_.oufile);
                        EndPro(ts.TotalSeconds.ToString("f2"));
                    }
                    else
                    {
                        FailPro();
                    }
                    ShowPath();
                }
                catch {
                    FailPro();
                }
            }
        }

        void Cal_FreeNet(object o)
        {
            Weight cov = (Weight)o;
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Multiselect = true;
            op.Filter = "Text Files(.in2)|*.in2";
            op.Title = "选择文件";
            op.ShowDialog(this);
            string[] paths = op.FileNames;
            if (paths.Length == 0) { }
            else
            {
                try
                {
                    Thread th = new Thread(StartPro);//开启进程
                    th.IsBackground = true;
                    th.Start();
                    Stopwatch spw = new Stopwatch();
                    spw.Start();
                    for (int i = 0; i < paths.Length; i++)
                    {
                        string path = paths[i];
                        FreeNet fn = new FreeNet();
                        fopt_.infile = path;
                        popt_.WgtDef = cov;
                        fn.FreeAdj(popt_, ref fopt_);
                        
                    }
                    spw.Stop();
                    TimeSpan ts = spw.Elapsed;

                    if (fopt_.oufile != "")
                    {
                        Process.Start("notepad.exe", fopt_.oufile);
                        EndPro(ts.TotalSeconds.ToString("f2"));
                    }
                    else
                    {
                        FailPro();
                    }
                    ShowPath();
                }
                catch {

                    FailPro();
                }
            }
        }

        //计算近似坐标
        void Approx()
        {
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Filter = "Text Files(.in2)|*.in2";
            op.Title = "选择平差文件";
            op.ShowDialog();
            string path = op.FileName;
            if (string.IsNullOrWhiteSpace(path)) { }
            else
            {
                try
                {
                    Plane pn = new Plane();
                    fopt_.infile = path;
                    pn.CalApprox(popt_, ref fopt_);
                    Process.Start("notepad",fopt_.xyhfile);
                }
                catch { }
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Filter = "*.in2|*.in2|*.ou2|*.ou2|*.ouf|*.ouf|*.gra|*.gra|*.txt|*.txt|" +
                "*.ous|*.ous|*.out|*.out|*.xyh|*.xyh|*.dat|*.dat|*.*|*.*";
            op.Title = "选择文件";
            op.ShowDialog();
            string path = op.FileName;
            if (string.IsNullOrWhiteSpace(path)) { }
            else
            {
                Process.Start("notepad", path);
            }
        }

        Form4 form4 = new Form4();
        private void 网图绘制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (form4 == null || form4.IsDisposed)
                form4 = new Form4();
            form4.Show();
            form4.Focus();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                popt_.UseKnApprox = 1;
                textBox5.Enabled = true;
            }
            else
            {
                popt_.UseKnApprox = 0;
                textBox5.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Filter = "近似坐标(.xyh)|*.xyh";
            op.Title = "选择平差文件";
            op.ShowDialog();
            string path = op.FileName;
            if (string.IsNullOrWhiteSpace(path)) { }
            else
            {
                try
                {
                    fopt_.approxfile = path;
                    textBox5.Text = path;
                }
                catch { }
            }
        }

        private void 平差处理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.RestoreDirectory = true;
            op.Multiselect = true;
            op.Filter = "Text Files(.in1)|*.in1";
            op.Title = "选择文件";
            op.ShowDialog();
            string[] paths = op.FileNames;
            if (paths.Length == 0) { }
            else
            {
                try
                {
                    Thread th = new Thread(StartPro);//开启进程
                    th.IsBackground = true;
                    th.Start();
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    for (int i = 0; i < paths.Length; i++)
                    {
                        string path = paths[i];
                        string outpath = "";
                        Height ht = new Height();
                        ht.Level(path, ref outpath);
                        Process.Start("notepad", outpath);
                    }
                    sw.Stop();
                    TimeSpan ts = sw.Elapsed;
                    EndPro(ts.TotalSeconds.ToString("f2"));
                }
                catch { }
            }
        }
    }

}
